package com.NeuglsWorkStudio.UI.Dialog
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.osmf.events.TimeEvent;

	/**实现一个ToastNotify通过。*/
	public class TToastNotify
	{
		private static var _FContainer:DisplayObjectContainer;
		private static var _FDisplayTime:Number=3000;
		private static var _FColor:int;
		private static var _FDisplaying:Boolean=false;
		
		public function TToastNotify()
		{
		}
		
		public static function Init(Container:DisplayObjectContainer,DisplayTime:Number,BaseColor:int):void{
			_FContainer=Container;
			_FDisplayTime=DisplayTime*1000;
			_FColor=BaseColor;
		}
		
		public static function ToToastNotify(Msg:String):void{
			if(_FDisplaying) return;
			if(_FContainer==null) return;
			
			_FDisplaying=true;
			var toastNotifier:TToastNotifier=new TToastNotifier(_FColor,40,Msg);
			
			var XX:Number=_FContainer.width;
			var YY:Number=_FContainer.height;
			if(_FContainer is Stage){
				XX=(_FContainer as Stage).stageWidth;
				YY=(_FContainer as Stage).stageHeight;
			}
			
			with(_FContainer.addChild(toastNotifier)){
				x=(XX-width)/2;
				y=(YY-height)/4;
				alpha=0.1;
			}
			
			TweenLite.to(toastNotifier,0.4,{alpha:1,onComplete:ToDisplay,onCompleteParams:[toastNotifier]});
		}
		
		private static function ToDisplay(T:TToastNotifier):void{
			var timer:Timer=new Timer(_FDisplayTime,1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,function(E:TimerEvent):void{
				ToDisappear(T,timer);
			});
			timer.start();
		}
		
		private static function ToDisappear(T:TToastNotifier,t:Timer):void{
			TweenLite.to(T,0.4,{alpha:0,onComplete:ToFree,onCompleteParams:[T]});
			t.stop();
			t=null;
		}
		
		private static function ToFree(T:TToastNotifier):void{
			_FContainer.removeChild(T);
			T=null;	
			_FDisplaying=false;
		}
	}
}